package com.ruoyi.crm.modules.mq;

import com.ruoyi.crm.domain.Contract;
import com.ruoyi.crm.modules.email.CrmMailService;
import com.ruoyi.crm.modules.sms.CrmSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class CrmMqConsumer {
    @Autowired
    private CrmMailService crmMailService;

    @Autowired
    private CrmSmsService crmSmsService;

    /**
     * 处理合同创建消息，发送邮件和短信通知给用户
     */
    @RabbitListener(queues = {CrmRabbitConfig.CRM_QUEUE_CONTRACT_CREATED})
    public void consumeContractCreated(Contract contract) {
        // 发送邮件
        if (StringUtils.hasText(contract.getEmail())) {
            try {
                crmMailService.sendContractCreatedMail(contract);
            } catch (Throwable throwable) {
                log.error(throwable.getMessage(), throwable);
            }
        }
        // 发送短信
        if (StringUtils.hasText(contract.getPhone())) {
            try {
                crmSmsService.sendMessage(contract.getPhone(), "短信内容", "1", new String[]{contract.getName(), contract.getContractOrder() + ""});
            } catch (Throwable throwable) {
                log.error(throwable.getMessage(), throwable);
            }
        }
    }
}
